package cn.com.nes.site.module.dao;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.common.util.OssUtil;
import cn.com.nes.common.util.Page;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.PageData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Repository
public class AdDaoImpl extends BaseDaoImpl {

    //热链云获取广告列表
    public PageData getAdList(Connection conn, DataReq req) throws SQLException {
        PageData res = new PageData();
        res.setRetCode(9);
        res.setRetMessage("获取信息失败！");
        StringBuffer sb = new StringBuffer();
        StringBuffer sbfrom = new StringBuffer() ;
        String AdType = (String) req.getReqData().get("AdType");
        String id = (String) req.getReqData().get("id");
        try {
            sb.append(" SELECT * from ad where isdelete='0' ");
            if(!StringUtils.isEmpty(AdType)) {
                sb.append(" and adtype= '").append(AdType).append("' ");
            }
            if(!StringUtils.isEmpty(id)) {
                sb.append(" and id= '").append(id).append("' ");
            }
            sb.append(" order by createtime desc ");
            sb = DBUtils.sql_check(sb) ;
            sbfrom.append(sb.toString()) ;
            int idx=sbfrom.indexOf(" from ") ;
            sbfrom.delete(0, idx);
            Page page = new Page(conn,sbfrom.toString(),req.getPageid(),req.getPagesize(),res,new Object[]{}) ;
            sb.append(" limit ").append(page.getOffsetlimit()).append(",").append(req.getPagesize()) ;
            List<Map<String, Object>> list = this.queryForList(conn,sb.toString(), new Object[]{});
            if(!StringUtils.isEmpty(id)){
                if(list.size()>0){
                    String imgurl=list.get(0).get("imgurl").toString();
                    String imgurlArr[]=imgurl.split(";");
                    String imgurlRel="";
                    for(int i=0;i<imgurlArr.length;i++){
                        String imgurlRelStr= OssUtil.getImgSignURL(imgurlArr[i],1);
                        imgurlRel=imgurlRel+imgurlRelStr+";";
                    }
                    list.get(0).put("imgurlRel",imgurlRel.substring(0,imgurlRel.length()-1));
                }
            }
            res.setPage(list);
            res.setRetCode(0);
            res.setRetMessage("ok");
        } finally {

        }
        return res;
    }

    //热链云保存广告信息
    public PageData saveAd(Connection conn,DataReq req) throws SQLException {
        Map<String, Object> param = req.getReqData();
        PageData res = new PageData();
        res.setRetCode(9);
        res.setRetMessage("参数信息保存失败");
        param.put("tableName", "ad");
        PageData sqlInfo =null;
        String id=(String)param.get("id");

        if (id ==null || "".equals(id)){
            sqlInfo = getInsertSQL(conn,req);
        }else{
            param.put("keys", "id") ;
            sqlInfo = getUpdateSQL(conn,req);
        }
        if (sqlInfo.getRetCode() != 0) {
            return sqlInfo;
        }
        final StringBuffer sb_field = (StringBuffer) sqlInfo.getData().get("sb_field");
        final List<String> listData = (List<String>) sqlInfo.getData().get("listData");
        this.update(conn,sb_field.toString(),listData.toArray());

        res.setRetCode(0);
        res.setRetMessage("OK");
        return res;
    }

    //热链云删除广告信息
    public PageData delAd(Connection conn,DataReq req) throws SQLException {
        Map<String, Object> param = req.getReqData();
        PageData res = new PageData();
        res.setRetCode(9);
        res.setRetMessage("删除参数信息失败");
        String id=(String)param.get("id");
        if(!StringUtils.isEmpty(id) ){
            StringBuffer sb = new StringBuffer();
            sb.append(" UPDATE ad SET isdelete='1' where id='").append(id).append("' ");
            this.update(conn,sb.toString(),new Object[] {});
            res.setRetCode(0);
            res.setRetMessage("OK");
        }else{
            return res;
        }

        return res;
    }

    //小程序获取对应广告
    public PageData getAd(Connection conn, DataReq req) throws SQLException {
        PageData res = new PageData();
        res.setRetCode(9);
        res.setRetMessage("获取信息失败！");
        StringBuffer sb = new StringBuffer();
        String AdType = (String) req.getReqData().get("AdType");
        if(AdType == null) {
            return res;
        }
        try {
            sb.append(" SELECT * from ad where  =? and endTime>now() ");
            List<Map<String, Object>> list = this.queryForList(conn, sb.toString(), new Object[]{AdType});
            if(list.size()>0){
                String imgurl=list.get(0).get("imgurl").toString();
                String imgurlArr[]=imgurl.split(";");
                String imgurlRel="";
                for(int i=0;i<imgurlArr.length;i++){
                    String imgurlRelStr= OssUtil.getImgSignURL(imgurlArr[i],1);
                    imgurlRel=imgurlRel+imgurlRelStr+";";
                }
                list.get(0).put("imgurlRel",imgurlRel.substring(0,imgurlRel.length()-1));
            }
            res.setPage(list);
            res.setRetCode(0);
            res.setRetMessage("ok");
        } finally {

        }
        return res;
    }

}